var ns;
Vue.component('tree-item', {
    template: '#item-template',
    props: {
        item: {}
    },
    data: function () {
        return {
            isOpen: true,
            selecttypeid: app.TypeID
        }
    },
    computed: {
        isFolder: function () {
            return this.item.children &&
                this.item.children.length
        }
    },
    methods: {
        toggle: function (item) {
            if (this.isFolder) {
                this.isOpen = !this.isOpen
            } else {
                this.choose_type(item);
            }
        },
        choose_type: function (item) {
            var that = this;
            app.TypeID = item.ID;
            app.TypeName = item.text;
            setTimeout(function () {
                app.is_type_list = false;
            }, 100)
        },
    }
})
var app = new Vue({
    el: '#app',
    data: {
        list: [],
        // ServiceType: 1, //1-客服工单 2-投诉建议 3-品控管理 4-审批中心
        PublishType: 1, //1-派单 2-工单转移 3-工单协同 4-重派 11-告警派单 12-告警转交  13-告警协同
        source: '',//来源 chooseuser:选择负责人
        gjType:0,//9-来自告警类派单
        ID: 0,
        select_id_list: [],
        category_list: [],
        is_xietong: true,
        is_type_list: false,
        is_tag_list: false,
        is_category_list: false,
        typelist: [],
        TypeID: 0,
        TypeName: '请选择',
        taglist: [],
        TagID: 0,
        TagName: '请选择',
        categorylist: [],
        CategoryID: 0,
        CategoryName: '请选择',
        showchoose: false,
        showsearch: false,//遮罩层
        chooseList: [],
        form: {
            keywords: '',
        },
    },
    methods: {
        show_morehistory: function (item) {
            var that = this;
            item.isopen = !item.isopen;
        },
        doHideSearch: function () {
            var that = this;
            that.showchoose = false;
            that.showsearch = false;
        },
        showChooseAll: function () {
            var that = this;
            that.showchoose = !that.showchoose;
            that.showsearch = !that.showsearch;//底部阴影

        },
        doChoosUser: function (item) {
            var that = this;
            //单选
            if (that.source == 'chooseuser') {
                that.chooseList = [];
                that.chooseList.push(item);
            }
            //多选
            else {
                item.isselected = !item.isselected;
                if (item.isselected) {
                    that.chooseList.push(item);
                }
                else {
                    const index = that.chooseList.findIndex(p => p.ID === item.id && p.TypeID === item.TypeID);
                    that.chooseList.splice(index, 1)
                }
                const new_list = [...new Set(that.chooseList)];// 去重
                that.chooseList = new_list;
            }
        },
        get_data: function () {
            var that = this;
            var options = {
                keywords: that.form.keywords,
            };
            if(that.gjType==9){
                ns.post(options, function (succeed, data, err) {
                    console.log('告警---'+JSON.stringify(data));
                    if(succeed){
                        that.list = data;
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }, {
                    toast: true,
                    command: 'App/Warning/GetRepairers'
                });
            }else{
                ns.post(options, function (succeed, data, err) {
                    console.log(JSON.stringify(data));
                    if(succeed){
                        that.list = data;
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }, {
                    toast: true,
                    command: 'App/Service/GetRepairers'
                });

            }
            


        },
        clear_searchVal: function () {
            var that = this;
            document.getElementById("search-input").value = '';
        },
        do_search: function () {
            this.get_data();
        },
        do_select_publishtype: function (type) {
            this.PublishType = type;
        },
        choose_tag: function (item) {
            var that = this;
            that.TagID = item.ID;
            that.TagName = item.Name;
            that.is_tag_list = false;
        },
        choose_category: function (item) {
            var that = this;
            that.CategoryID = item.ID;
            that.CategoryName = item.Name;
            that.is_category_list = false;
        },
        tel_phone: function (item) {
            api.call({
                type: 'tel_prompt',
                number: item.phone
            });
        },
        open_type_list: function () {
            var that = this;
            that.is_tag_list = false;
            that.is_category_list = false;
            that.is_type_list = !that.is_type_list;
        },
        open_tag_list: function () {
            var that = this;
            that.is_type_list = false;
            that.is_tag_list = !that.is_tag_list;
            that.is_category_list = false;
        },
        open_category_list: function () {
            var that = this;
            that.is_type_list = false;
            that.is_tag_list = false;
            that.is_category_list = !that.is_category_list;
        },
        do_choose: function (item, list) {
            item.isselected = !item.isselected;
        },
        set_select_items: function () {
            var that = this;
            that.select_id_list = [];
            that.category_list = [];
            for (var i = 0; i < that.chooseList.length; i++) {
                var item = that.chooseList[i];
                that.select_id_list.push(item.id);
            }
        },

        do_save: function () {
            var that = this;
            that.set_select_items();
            if (that.select_id_list.length == 0) {
                api.toast({
                    msg: '请选择服务人员',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var options = {};
            options.Id = that.ID;
            options.repairerIds = JSON.stringify(that.select_id_list);
            //派单
            if(that.PublishType==1){
                ns.post(options, function (succeed, data, err) {
                    if(succeed){
                       that.do_succeed();
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    setTimeout(function () {
                        api.closeWin();
                    }, 500)
                }, {
                    toast: true,
                    toastmsg: '提交中',
                    command:'App/Service/Deliver' 
                });
            }
            //转单
            if(that.PublishType==3){
                ns.post(options, function (succeed, data, err) {
                    if(succeed){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name: 'reloadservicelist',
                        });
                        api.sendEvent({
                            name: 'updata_task_con_close',
                        });
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    setTimeout(function () {
                        api.closeWin();
                    }, 500)
                }, {
                    toast: true,
                    toastmsg: '提交中',
                    command:'App/Service/Transfer' 
                });
            }
            //协同
            if(that.PublishType==2){
                ns.post(options, function (succeed, data, err) {
                    if(succeed){
                       that.do_succeed();
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    setTimeout(function () {
                        api.closeWin();
                    }, 500)
                }, {
                    toast: true,
                    toastmsg: '提交中',
                    command:'App/Service/Teamwork' 
                });
            }
            //告警派单
            if(that.PublishType==11){
                ns.post(options, function (succeed, data, err) {
                    
                    if(succeed){
                       that.do_succeed();
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    setTimeout(function () {
                        api.closeWin();
                    }, 500)
                }, {
                    toast: true,
                    toastmsg: '提交中',
                    command:'App/Warning/Deliver' 
                });
            }
            //告警转交
            if(that.PublishType==12){
                ns.post(options, function (succeed, data, err) {

                    if(succeed){
                       that.do_succeed();
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    setTimeout(function () {
                        api.closeWin();
                    }, 500)
                }, {
                    toast: true,
                    toastmsg: '提交中',
                    command:'App/Warning/Transfer' 
                });
            }
            //告警协同
            if(that.PublishType==13){
                ns.post(options, function (succeed, data, err) {
                    console.log(JSON.stringify(data));
                    console.log(JSON.stringify(err));
                    if(succeed){
                       that.do_succeed();
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    setTimeout(function () {
                        api.closeWin();
                    }, 500)
                }, {
                    toast: true,
                    toastmsg: '提交中',
                    command:'App/Warning/Teamwork' 
                });
            }
            
            return;
        },

        do_succeed:function(){
            var that=this;
            api.toast({
                msg: '操作成功',
                duration: 2000,
                location: 'bottom'
            });
            api.sendEvent({
                name: 'reloadservicelist',
            });
            api.sendEvent({
                name: 'updata_task_con',
            });

        },





        toChooseUser: function () {
            var that = this;
            // 单选派单，重派
            if (that.ID > 0) {
                that.do_save();
            }
            // 新增 选择负责人
            else {
                api.sendEvent({
                    name: 'updata_choose_user',
                    extra: {
                        data: that.chooseList,
                    }
                });
                setTimeout(function () {
                    api.closeWin();
                }, 500)
            }
        },
        //整改任务重派单
        toRepostUser: function () {
            var that = this;
            that.set_select_items();
            var options = {};
            if(that.PublishType==2){
                options.action = "qulitycontrolchangeuser";
                options.ID = that.ID;
                options.UserID = that.category_list[0].ID;
                options.UserName = that.category_list[0].text;
            }else{
                options.action = "refreshpublishquality";
                options.ID = that.ID;
                options.AcceptUserID = that.category_list[0].ID;
                options.AcceptUserName = that.category_list[0].text;
            }
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '操作成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                    api.sendEvent({
                        name: 'updata_task_con',
                    });
                    api.sendEvent({
                        name: 'close_detail',
                    });
                    setTimeout(function () {
                        api.closeWin();
                    }, 500)

                } else {
                    api.toast({
                        msg: '操作失败',
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            })
        }
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_search();
    app.ID = api.pageParam.id || 0;
    app.PublishType = api.pageParam.type || 0;
    app.gjType = api.pageParam.gjType||0;
    console.log(JSON.stringify(app.PublishType));
    // app.ServiceType = api.pageParam.ServiceType || 1;
    app.source = api.pageParam.source;
    app.is_xietong = api.pageParam.is_xietong || false;
    setTimeout(function () {
        app.get_data();
    }, 500)
};